package com.youngjun;

import com.youngjun.util.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableAsync;


@SpringBootApplication
@EnableCaching
public class DemoApplication {

    private final static Logger logger = LoggerFactory.getLogger(DemoApplication.class);

    public static void main(String[] args) {
        new SpringApplicationBuilder(DemoApplication.class).beanNameGenerator((definition, registry) -> definition.getBeanClassName()).run(args);
    }

    @Bean
    public IdWorker idWorker() {
        long workerId=1;
        long dataCenterId=1;
        try {
            long ip = IPUtils.ipV4ToLong(IPUtils.getIp());
            ip = ip % 256;
            workerId = ip%31+1;
            dataCenterId = ip/31+1;
        }catch (Exception ignored){
            ignored.printStackTrace();
        }
        return new IdWorker(workerId, dataCenterId,1);
    }

    @Bean
    public ApplicationUtils applicationUtils(ApplicationContext applicationContext) {
        return ApplicationUtils.init(applicationContext);
    }

}
